package structures;

import java.io.Serializable;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import util.GlobalUtils;
import util.HibernateUtil;
import constants.Globals;
import domain.LogServiceCallDAO;
import exception.IBToolsException;

public class HistoryData implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4673966418937408267L;
	
	private Long oid;
	private String dateTime;
	private String environment;
	private String errorType;
	private String serviceName;
	private boolean success;
	private String userCode;
	private String xmlString;
	private String errorText;
	
	public HistoryData(Object[] historyData) {
		super();
		this.dateTime = nvl(historyData[0]);
		this.environment = nvl(historyData[1]);
		this.errorType = nvl(historyData[2]);
		this.serviceName = nvl(historyData[3]);
		this.success = nvl(historyData[4]).equalsIgnoreCase("1");
		this.userCode = nvl(historyData[5]);
		this.oid = Long.parseLong(nvl(historyData[8]));
		this.errorText = GlobalUtils.getSizeLimitedText(nvl(historyData[7]), 57);
		try {
			this.xmlString = resizeText(nvl(historyData[6]), 57);
		} catch (IBToolsException e) {
			e.printStackTrace();
		}
	}

	public LogServiceCallDAO loadHistoryWithOid() throws IBToolsException{
		LogServiceCallDAO data = new LogServiceCallDAO();
		data.setOid(oid);
		return data;
	}
	
	public LogServiceCallDAO loadHistoryFromDB() throws IBToolsException{

    	Session session = HibernateUtil.getDEVSession();
        Transaction tx = null;
        LogServiceCallDAO response = null;
        try {
			tx = HibernateUtil.beginDEVTransaction(session);
			Criteria criteria = session.createCriteria(LogServiceCallDAO.class)
					.add(Restrictions.eq("oid", oid));

			response = (LogServiceCallDAO)criteria.uniqueResult();
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			throw e;
		}
        return response;
	}
	
	private String nvl(Object o){
		String response = Globals.EMPTY_STRING;
		if(o!=null)
			response = o.toString();
		return response;
	}
	
	public String getDateTime() {
		return dateTime;
	}
	public void setDateTime(String dateTime) {
		this.dateTime = dateTime;
	}
	public String getEnvironment() {
		return environment;
	}
	public void setEnvironment(String environment) {
		this.environment = environment;
	}
	public String getErrorType() {
		return errorType;
	}
	public void setErrorType(String errorType) {
		this.errorType = errorType;
	}
	public String getServiceName() {
		return serviceName;
	}
	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}
	
	public String getUserCode() {
		return userCode;
	}
	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}
	public String getXmlString() {
		return xmlString;
	}
	public void setXmlString(String xmlString) {
		this.xmlString = xmlString;
	}
	public String getErrorText() {
		return errorText;
	}
	public void setErrorText(String errorText) {
		this.errorText = errorText;
	}

	public Long getOid() {
		return oid;
	}

	public void setOid(Long oid) {
		this.oid = oid;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}
	
	private String resizeText(String text, int size) throws IBToolsException{
		text = text.replaceAll("\n", "");
		int ext = text.indexOf("<EXT");
		if( ext> -1)
			text = text.substring(ext);
		text = GlobalUtils.getSizeLimitedText(text, size);

		if(text.equalsIgnoreCase("")){
			text = "-";
		}
		return text;
	}
	
}
